<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="${pageContext.request.contextPath}/static/bootstrap3/css/bootstrap.min.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/static/bootstrap3/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/static/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/static/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/static/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script src="${pageContext.request.contextPath}/lay-ui/lib/layui-v2.6.3/layui.js"></script>
    <title>修改博客</title>
    <style type="text/css">
        a{ text-decoration:none}
    </style>
    <script type="text/javascript" >
        function submitData(){
            var title=$("#title").val();
            var blogTypeId=$("#blogTypeId option:selected").val();
            var content=UE.getEditor('editor').getContent();
            if(title==null || title==''){
                layer.msg("请输入标题！");
            }else if(blogTypeId==null || blogTypeId==''){
                layer.msg("请选择博客类别！");
            }else if(content==null || content==''){
                layer.msg("请填写内容！");
            }else{
                layer.confirm('您确定要修改吗？',{
                    btn:['是','否']
                },function (){
                $.post("${pageContext.request.contextPath}/admin/blog/save.do",
                    {
                        'id':'${param.id}',
                        'title':title,
                        'blogType.id':blogTypeId,
                        'content':content,
                        'contentNoTag':UE.getEditor('editor').getContentTxt(),//获取博客text内容
                        'summary':UE.getEditor('editor').getContentTxt().substr(0,155)
                    },function(result){
                        if(result.success){
                            layer.msg("博客修改成功！");
                            resultValue();
                        }else{
                            layer.msg("博客发布失败！");
                        }
                    },"json");
            }
                )}
        }
    </script>
    <style>
        body {
            background-image: url(/web/image/fg3.jpeg);
            background-repeat: repeat-y;
            background-size: 100%;
            background-position: 0px -200px;
            position: relative;
            font-family: "microsoft yahei", Verdana, Arial, 微软雅黑;
        }
    </style>
</head>
<body>
<div class="col-sm-10" style="background-color: rgba(255,255,255,0.5);width: 100%">
    <p></p>
    <ol class="breadcrumb" style="background-color: rgba(255,255,255,0);margin-left: 55px">
        <li class="active" style="font-size:22px;color: #2E2D3C">Update Blog
        </li>
    </ol>
    <form class="form-horizontal">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">博客标题：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="title" placeholder="您的标题：">
            </div>
        </div>
        <div class="form-group">
            <label for="blogTypeId" class="col-sm-2 control-label">所属类别：</label>
            <div class="col-sm-9">
                <select   id="blogTypeId" name="blogType.id" class="form-control" style="width: 155px">
                    <option value="">请选择博客类别...</option>
                    <c:forEach var="blogType" items="${blogTypeCountList }">
                        <option value="${blogType.id }">${blogType.typeName }</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="edi" class="col-sm-2 control-label">博客内容：</label>
            <div class="col-sm-9">
                <div id="edi" > <script id="editor" type="text/plain" style="width:100%;height:500px;"></script></div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <a href="javascript:submitData()" class="btn btn-default" role="button">修改博客</a>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    var ue = UE.getEditor('editor');
    ue.addListener("ready",function(){
        // 通过ajax请求数据
        UE.ajax.request("${pageContext.request.contextPath}/admin/blog/findById.do",
            {
                method:"post",
                async:false,
                data:{
                    "id":"${param.id}"},
                    onsuccess:function(result){
                        result=eval("("+result.responseText+")");
                        $("#title").val(result.title);
                        var province = document.getElementById("blogTypeId");
                        for(var i=0;i<province.options.length;i++){
                            if(province.options[i].value==result.blogType.id){
                                province.options[i].selected=true;
                            };
                        }

                        UE.getEditor('editor').setContent(result.content);
                }
            });
    });
</script>
</body>
</html>
